home *** CD-ROM | disk | FTP | other *** search
/ The Essential Home & Business Collection / The Essential Home & Business Collection.iso / 27 / 3 / 5 / HP22D5.ZIP / EXTERN / TEST.C < prev    next >
Text File  |  1991-04-16  |  6KB  |  209 lines

  1. /*
  2. ** test.c:        This C source code contains some examples of how
  3. **            extensions can be used from HPAD
  4. **
  5. ** To make the extension:
  6. **
  7. **    C> make test        <run the make file>
  8. **
  9. ** The make file performs the following compile/link steps:
  10. **
  11. ** C> cl /Alfw /c /Oalt /Gs test.c
  12. **
  13. **    - /Al use the large memory model
  14. **    - /Af use far code pointers
  15. **    - /Aw assume SS is not equal to DS (extensions share HPAD's stack)
  16. **    - /c compile only in this step
  17. **    - /Gs no stack checking (extensions CANNOT perform stack checking)
  18. **
  19. ** C> link test,,,startup/NOE;
  20. **
  21. **    - /NOE means no default library (extension use a custom printf,sprintf)
  22. */
  23.  
  24. #include <dos.h>
  25.  
  26. #include <stdarg.h>        /* include for variable number of arguments */
  27. #include "extern.h"        /* Extensions need these! */
  28.  
  29. /*
  30. ** WhenLoaded():    This function gets called immediately after the EXE
  31. **            has been relocated in HPAD's memory space.  It can 
  32. **            perform such tasks such as initializing memory space,
  33. **            taking over any interrupts, etc.  It cannot send
  34. **            any messages to HPAD!
  35. **
  36. ** Parameters:        none
  37. **
  38. ** returns:        none
  39. **
  40. ** notes:        If you don't use this function, you don't need to
  41. **            include it
  42. **
  43. */
  44. pascal far WhenLoaded()
  45.  
  46. {
  47. }
  48.  
  49. /*
  50. ** WhenLoaded():    This function gets called immediately after the EXE
  51. **            has been relocated in HPAD's memory space.  It can 
  52. **            perform such tasks such as freeing memory space,
  53. **            setting interrupts back, etc.  It cannot send
  54. **            any messages to HPAD!
  55. **
  56. ** Parameters:        none
  57. **
  58. ** returns:        none
  59. **
  60. ** notes:        If you don't use this function, you don't need to
  61. **            include it
  62. */
  63. pascal far WhenUnLoaded()
  64.  
  65. {
  66. }
  67.  
  68. /*
  69. ** SampleHandler():    This is a sample handler that is callable from HPAD.
  70. **            It demonstrates some of the capabilities of extensions.
  71. **
  72. ** Parameters:        Variable.  This function just prints out any parameters
  73. **            that get passed.
  74. **
  75. ** returns:        STOP = means that this message should not get passed
  76. **            on to the next layer in the object heirarchy.
  77. **
  78. ** Example:        handler select;
  79. **            begin
  80. **              SampleHandler 1,"Hello","World";
  81. **            end;
  82. */
  83. SampleHandler(int NumArgs,...)
  84.  
  85. {
  86.     va_list args;
  87.     HANDLE hdl;
  88.     int i;
  89.  
  90.     /* set up 'args' to point to the first argument... */
  91.     va_start(args,NumArgs);
  92.  
  93.     /*
  94.     /* cycle through the arguments, printing out their values...
  95.     ** This will mess up the screen, but won't hurt anything!
  96.     ** NOTE: HPAD takes care of freeing its passed parameters.
  97.     */
  98.     for (i=0;i<NumArgs;i++) {
  99.         hdl = va_arg(args,HANDLE);
  100.         printf("parameter %d = %s\n",i,*hdl);
  101.     }
  102.  
  103.     /*
  104.     ** send a message to HPAD to beep twice. This is done by converting
  105.     ** a string (the beep command) to a handle in HPAD's memory space.
  106.     ** then, the new handle is send to HPAD's message dispatcher through
  107.     ** a function called Do().
  108.     ** After Do() returns, we free the handle (This is important because
  109.     ** if the handle is not freed, the memory will be lost forever).
  110.     */
  111.     hdl = stoh("beep 2");
  112.     Do(hdl);
  113.     FreeHandle(hdl);
  114.  
  115.     /*
  116.     ** Get the contents of a field. This is done by calling GetFieldNum()
  117.     ** which returns the contents of the Nth field. Again, this function
  118.     ** returns a handle of a duplicate of the field's content. It is up
  119.     ** to us to free the handle when we are done.
  120.     */
  121.     hdl = GetFieldNum(    TRUE,        /* it is a field on the page */
  122.                 1        /* which field (first field) */
  123.             );
  124.  
  125.     /* print out the field's content, if there is a content */
  126.     if (hdl) {
  127.         printf("field 1 = %s\n",*hdl);    /* print it */
  128.         FreeHandle(hdl);            /* free the memory */
  129.     }
  130.  
  131.     /*
  132.     ** Set the content of background field 1. This is done by first
  133.     ** creating a handle that is to become the field's content. Then
  134.     ** passing it to HPAD along with which background field we want.
  135.     */
  136.     hdl = stoh("This is the new content");
  137.     SetFieldNum(    FALSE,        /* this is a background field */
  138.             1,        /* set field number 1 */
  139.             hdl        /* set to this handle */
  140.         );
  141.  
  142.     /* this causes HPAD not to pass the message on */
  143.     return(STOP);
  144. }
  145.  
  146. /*
  147. ** NumChars():        This is a sample function that count the occurrences of
  148. **            a character in a string.
  149. **
  150. ** Parameters:        SearchCh:    The character we are counting
  151. **            SearchStr:    The string we are searching
  152. **
  153. ** returns:        Count:        The number of occurrences of SearchCh
  154. **                    in the string SearchStr
  155. **
  156. ** Example:        handler select;
  157. **            begin
  158. **              -- this will put 2 into the message box
  159. **              put the NumChars of "o","Hello world";
  160. **            end;
  161. **
  162. */
  163. NumChars(int NumArgs,...)
  164.  
  165. {
  166.     va_list args;
  167.     HANDLE hdl;
  168.     int count,SearchCh;
  169.     char *SearchStr;
  170.  
  171.     /* check for invalid number of arguments */
  172.     if (NumArgs != 2) return(STOP);
  173.  
  174.     /* set up 'args' to point to the first argument... */
  175.     va_start(args,NumArgs);
  176.  
  177.     /* get the search character */
  178.     SearchCh = **va_arg(args,HANDLE);
  179.  
  180.     /* get a pointer to the search string */
  181.     SearchStr = *va_arg(args,HANDLE);
  182.  
  183.     /* perform the search */
  184.     for (count=0;*SearchStr;SearchStr++)
  185.         if (*SearchStr == SearchCh) count++;
  186.  
  187.     /* call integer-to-handle (itoh) to return the value to HPAD */
  188.     ReturnValue(itoh(count));
  189.  
  190.     /* don't pass the message on */
  191.     return(STOP);
  192. }
  193.  
  194. POOL pascal Pool[] = {
  195.     {    "Sample",        /* name of the handler */
  196.         SampleHandler,        /* pointer to the handler */
  197.         0,            /* reserved */
  198.         HANDLER},        /* this is a handler, not a function */
  199.  
  200.     {    "NumChars",        /* name of this function */
  201.         NumChars,        /* pointer to this function */
  202.         0,            /* reserved */
  203.         FUNCTION},        /* this is a function */
  204.  
  205.     {    NULL,            /* NULLs signify the end of the table */
  206.         NULL,
  207.         0,
  208.         0}    };
  209.